from config import BASE_PATH


def common_assert(resp, exp_status_code, exp_status, exp_description):
    assert exp_status_code == resp.status_code
    assert exp_status == resp.json().get("status")
    assert exp_description in resp.json().get("description")

import pymysql
import traceback
# 1.定义一个方法
def exe_sql(sql_str, database):
    conn = None
    cur = None
    try:
        # 2.创建数据库连接对象
        # 3.创建游标对象
        conn = pymysql.connect(host="121.43.169.97", user="root", password="Itcast_p2p_20191228",database=database)
        cur = conn.cursor()
        cur.execute(sql_str)
        if sql_str.split(" ")[0].lower()=="select":
            return cur.fetchall()
        else:
            conn.commit()
            return "受影响的行数:{}".format(cur.rowcount)
    except:
        conn.rollback()
        #打印异常日志
        traceback.print_exc()
    finally:
        if cur is not None:
            cur.close()
        if conn is not None:
            conn.close()
# 封装清洗用户信息的方法
def clear_user_info():
    phone_all = "'13816547001','13816547002','13816547003','13816547004','13816547005','13816547006','13816547007','13816547008','13816547009','13816547010','13816547011'"
    # 需要执行SQL语句
    sql1 = "delete i.* from mb_member_info i inner join mb_member m on m.id = i.member_id where m.phone in ({})".format(phone_all)
    sql2 = "delete l.* from mb_member_login_log l inner join mb_member m on m.id=l.member_id where m.phone in ({})".format(phone_all)
    sql3 = "delete from mb_member where phone in ({})".format(phone_all)
    sql4 = "delete from mb_member_register_log where phone in({})".format(phone_all)
    # 调用封装操作数据库的方法，执行SQL语句
    print(exe_sql(sql1, "czbk_member"))
    print(exe_sql(sql2, "czbk_member"))
    print(exe_sql(sql3, "czbk_member"))
    print(exe_sql(sql4, "czbk_member"))
import json
# 封装读取测试数据的方法
def read_json(filename,apiname):
    # 获取测试数据的文件路径
    file_path = BASE_PATH+"/data/"+filename+".json"

    # 读取测试数据
    with open(file_path,encoding="utf-8") as f:
        data =json.load(f)
        # 将测试数据解析为字典类型
    api_data = data.get(apiname)
    # print(api_data)
    # 获取指定接口的测试数据
    # 定义一个新的列表
    list_data =[]
    # 列表嵌套字典的测试数据转化为列表嵌套元组
    for i in api_data:
      tem= tuple(i.values())
      list_data.append(tem)
    # 返回最新格式的测试数据
    # print("最终的数据",list_data)
    return list_data
from bs4 import BeautifulSoup
# html解析工具
def html_util(response):
    # 1.从响应结果中，获取请求的标签数据
    data = response.json().get("description").get("form")
    # 2.创建beautifulsoup对象
    bs = BeautifulSoup(data,"html.parser")
    # 3.从form标签中获取url
    url = bs.form.get("action")
    # 4. 从input标签中获取请求体数
    # 4.1 定义一个字典，用来接收请求体的数据
    form_dict = {}
    # 4.2 获取全部的input标签
    input_all = bs.find_all("input")
    # 4.3 循环遍历出每一个input标签
    for i in input_all:
        # 4.4从input标签中获取请求体的字段名
        key = i.get("name")
        # 4.5从input标签中获取请求体的字段值
        value = i.get("value")
        # 4.6.将字段名和值放入定义的请求体数据的字典中
        form_dict[key]=value

    # 7.返回，带有url和请求体数据的列表
    return [url,form_dict]


if __name__ == '__main__':
    read_json("reg_login_data","img_verify_code")